// Copyright 2011, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v201101;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.v201101.cm.CampaignTargetOperation;
import com.google.api.adwords.v201101.cm.CampaignTargetReturnValue;
import com.google.api.adwords.v201101.cm.CampaignTargetServiceInterface;
import com.google.api.adwords.v201101.cm.CountryTarget;
import com.google.api.adwords.v201101.cm.GeoTarget;
import com.google.api.adwords.v201101.cm.GeoTargetList;
import com.google.api.adwords.v201101.cm.LanguageTarget;
import com.google.api.adwords.v201101.cm.LanguageTargetList;
import com.google.api.adwords.v201101.cm.Operator;
import com.google.api.adwords.v201101.cm.TargetList;

/**
 * This example adds geo, language, and network targeting to an existing
 * campaign. To get a campaign, run GetAllCampaigns.java.
 *
 * Tags: CampaignTargetService.mutate
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class SetCampaignTargets {
  public static void main(String[] args) {
    try {
      // Log SOAP XML request and response.
      AdWordsServiceLogger.log();

      // Get AdWordsUser from "~/adwords.properties".
      AdWordsUser user = new AdWordsUser();

      // Get the CampaignTargetService.
      CampaignTargetServiceInterface campaignTargetService =
          user.getService(AdWordsService.V201101.CAMPAIGN_TARGET_SERVICE);

      long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");

      // Create language targets.
      LanguageTargetList languageTargetList = new LanguageTargetList();
      languageTargetList.setCampaignId(campaignId);
      languageTargetList.setTargets(
          new LanguageTarget[] {new LanguageTarget(null, "fr"), new LanguageTarget(null, "ja")});

      // Create geo targets.
      GeoTargetList geoTargetList = new GeoTargetList();
      geoTargetList.setCampaignId(campaignId);
      geoTargetList.setTargets(
          new GeoTarget[] {new CountryTarget(null, false, "US"),
              new CountryTarget(null, false, "JP")});

      // Create operations.
      CampaignTargetOperation languageTargetOperation = new CampaignTargetOperation();
      languageTargetOperation.setOperand(languageTargetList);
      languageTargetOperation.setOperator(Operator.SET);

      CampaignTargetOperation geoTargetOperation = new CampaignTargetOperation();
      geoTargetOperation.setOperand(geoTargetList);
      geoTargetOperation.setOperator(Operator.SET);

      CampaignTargetOperation[] operations =
          new CampaignTargetOperation[] {languageTargetOperation, geoTargetOperation};

      // Set campaign targets.
      CampaignTargetReturnValue result = campaignTargetService.mutate(operations);

      // Display campaign targets.
      if (result != null && result.getValue() != null) {
        for (TargetList targetList : result.getValue()) {
          System.out.println("Campaign target with campaign id \"" + targetList.getCampaignId()
              + "\" and of type \"" + targetList.getTargetListType() + "\" was set.");
        }
      } else {
        System.out.println("No campaign targets were set.");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
